package com.which.one;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

public class LunPanActivity extends AppCompatActivity {

    private EditText clickCountEditText;
    private EditText bulletCountEditText;
    private Button startButton;
    private Button spinButton;
    private TextView resultTextView;
    private int totalClicks;
    private int bulletPosition;
    private int currentClick;
    private boolean gameStarted;
    private Random randomGenerator = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lunpan);

        clickCountEditText = findViewById(R.id.clickCountEditText);
        bulletCountEditText = findViewById(R.id.bulletCountEditText);
        startButton = findViewById(R.id.startButton);
        spinButton = findViewById(R.id.spinButton);
        resultTextView = findViewById(R.id.resultTextView);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!gameStarted) {
                    startGame();
                }
            }
        });

        spinButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                spin();
            }
        });
    }

    private void startGame() {
        String clickCountStr = clickCountEditText.getText().toString();
        String bulletCountStr = bulletCountEditText.getText().toString();

        if (clickCountStr.isEmpty() || bulletCountStr.isEmpty()) {
            Toast.makeText(this, "请输入总可点击数和子弹数", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            totalClicks = Integer.parseInt(clickCountStr);
            int bulletCount = Integer.parseInt(bulletCountStr);

            if (totalClicks <= 0 || bulletCount <= 0 || bulletCount > totalClicks) {
                Toast.makeText(this, "总可点击数必须大于0且子弹数不能超过总可点击数", Toast.LENGTH_SHORT).show();
                return;
            }

            bulletPosition = randomGenerator.nextInt(totalClicks);
            currentClick = 0;
            gameStarted = true;

            clickCountEditText.setEnabled(false);
            bulletCountEditText.setEnabled(false);
            startButton.setVisibility(View.GONE);
            spinButton.setVisibility(View.VISIBLE);
            resultTextView.setText("");

            Toast.makeText(this, "游戏已开始", Toast.LENGTH_SHORT).show();
        } catch (NumberFormatException e) {
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }

    private void spin() {
        currentClick++;

        if (currentClick == bulletPosition + 1) {
            // Player loses
            gameStarted = false;
            getWindow().getDecorView().setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
            resultTextView.setText("你输了！");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("游戏结束")
                    .setMessage("你输了！")
                    .setPositiveButton("确定", (dialog, which) -> finish());
            builder.show();
        } else if (currentClick >= totalClicks) {
            // Player wins
            gameStarted = false;
            resultTextView.setText("恭喜你赢了！");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("游戏结束")
                    .setMessage("恭喜你赢了！")
                    .setPositiveButton("确定", (dialog, which) -> finish());
            builder.show();
        } else {
            resultTextView.setText("第 " + currentClick + " 次旋转 - 幸运！");
        }
    }
}
